package com.kexin.student.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.kexin.common.constant.UserConstants;
import com.kexin.common.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kexin.common.annotation.Log;
import com.kexin.common.core.controller.BaseController;
import com.kexin.common.core.domain.AjaxResult;
import com.kexin.common.enums.BusinessType;
import com.kexin.student.domain.Studept;
import com.kexin.student.service.IStudeptService;
import com.kexin.common.utils.poi.ExcelUtil;
import com.kexin.common.core.page.TableDataInfo;

/**
 * 学生部门Controller
 * 
 * @author ruoyi
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/studept/studept")
public class StudeptController extends BaseController
{
    @Autowired
    private IStudeptService studeptService;

    /**
     * 查询学生部门列表
     */
    @PreAuthorize("@ss.hasPermi('studept:studept:list')")
    @GetMapping("/list")
    public AjaxResult list(Studept studept)
    {
        List<Studept> studepts = studeptService.selectStudeptList(studept);
        return success(studepts);
    }
    /**
     * 查询学生部门列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('studept:studept:list')")
    @GetMapping("/list/exclude/{deptId}")
    public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId)
    {
        List<Studept> studepts = studeptService.selectStudeptList(new Studept());
        studepts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
        return success(studepts);
    }

    /**
     * 导出学生部门列表
     */
    @PreAuthorize("@ss.hasPermi('studept:studept:export')")
    @Log(title = "学生部门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Studept studept)
    {
        List<Studept> list = studeptService.selectStudeptList(studept);
        ExcelUtil<Studept> util = new ExcelUtil<Studept>(Studept.class);
        util.exportExcel(response, list, "学生部门数据");
    }

    /**
     * 获取学生部门详细信息
     */
    @PreAuthorize("@ss.hasPermi('studept:studept:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable Long deptId)
    {
        studeptService.checkStudeptDataScope(deptId);
        return success(studeptService.selectStudeptById(deptId));
    }

    /**
     * 新增学生部门
     */
    @PreAuthorize("@ss.hasPermi('studept:studept:add')")
    @Log(title = "学生部门", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Studept studept)
    {
        if (!studeptService.checkStudeptNameUnique(studept))
        {
            return error("新增部门'" + studept.getDeptName() + "'失败，部门名称已存在");
        }
        studept.setCreateBy(getUsername());
        return toAjax(studeptService.insertStudept(studept));
    }
    /**
     * 修改学生部门
     */
    @PreAuthorize("@ss.hasPermi('studept:studept:edit')")
    @Log(title = "学生部门", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Studept studept)
    {
        Long deptId = studept.getDeptId();
        studeptService.checkStudeptDataScope(deptId);
        if (!studeptService.checkStudeptNameUnique(studept))
        {
            return error("修改部门'" + studept.getDeptName() + "'失败，部门名称已存在");
        }
        else if (studept.getParentId().equals(deptId))
        {
            return error("修改部门'" + studept.getDeptName() + "'失败，上级部门不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, studept.getStatus()) && studeptService.selectNormalChildrenStudeptById(deptId) > 0)
        {
            return error("该部门包含未停用的子部门！");
        }
        studept.setUpdateBy(getUsername());
        return toAjax(studeptService.updateStudept(studept));
    }
    /**
     * 删除学生部门
     */
    @PreAuthorize("@ss.hasPermi('studept:studept:remove')")
    @Log(title = "学生部门", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable Long deptId)
    {
        if (studeptService.hasChildByDeptId(deptId))
        {
            return warn("存在下级部门,不允许删除");
        }
        if (studeptService.checkStudeptExistUser(deptId))
        {
            return warn("部门存在用户,不允许删除");
        }
        studeptService.checkStudeptDataScope(deptId);
        return toAjax(studeptService.deleteStudeptById(deptId));
    }
}
